package com.frame.platform.upload;

import com.frame.common.SystemConfig;
import com.frame.database.JdataBean;
import com.frame.pageform.*;
import com.frame.pageform.component.JsonObject;
import com.wframe.project.jnd.extinterface.oss.AliyunOssUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.StringUtils;

public class UploadPage extends PageForm{

    static{
        setForward("icon","/platform/upload/selectIcon.jsp");
        setForward("img","/platform/upload/selectImg.jsp");
        setForward("file","/platform/upload/selectFile.jsp");
        setForward("ok","/platform/upload/uploadOk.jsp");
    }
    private String rootPath;
    private String fileName;
    private String type;
    private String actions;
    private String state;
    private List<String> fileList;
    private String tag;

    public String execute() throws Exception {
        // TODO Auto-generated method stub
//        if(!rootPath.endsWith("/"))
//            rootPath+="/";
//        String contextPath=RequestContext.getContext().getRequest().getContextPath();
//        
//        String path=RequestContext.getContext().getRealPath(rootPath);
//        rootPath=contextPath+rootPath;
        if(getType()==null)
            setType("img");
        if("img".equals(getType())){
            List<String> getfileList=new AliyunOssUtils().getFileList(rootPath);
            fileList=new ArrayList<String>();
            List<JdataBean> dlist=new FileUpDao().getDelList(rootPath);
            boolean bl=true;
            for(int i=0;i<getfileList.size();i++){
                bl=true;
                for(JdataBean dfile:dlist){
                    if(dfile.getString("filepath").equals(getfileList.get(i))){
                        bl=false;
                        break;
                    }
                }
                if(bl)fileList.add(getfileList.get(i));
            }
        }else{
            if(!rootPath.endsWith("/"))
                rootPath+="/";
            String contextPath=RequestContext.getContext().getRequest().getContextPath();

            String path=RequestContext.getContext().getRealPath(rootPath);
            rootPath=contextPath+rootPath;
            fileList=FileUpload.getInstance().getFileNameList(path, new String[]{"gif","jpg","png","bmp"}, true);
        }

//        if(getType().equals("img") || getType().equals("icon"))
//            fileList=FileUpload.getInstance().getFileNameList(path, new String[]{"gif","jpg","png","bmp"}, true);
//        else if(getType().equals("file"))
//            fileList=FileUpload.getInstance().getFileNameList(path,null,true);

        return forward(getType());
    }

    public String upload() throws IOException, Exception{
        SimpleDateFormat sp=new SimpleDateFormat("yyyyMMddHHmmss");
        String contextPath=RequestContext.getContext().getRequest().getContextPath();
        rootPath=rootPath.replaceFirst(contextPath, "");
        JsonObject json=new JsonObject();
        if(getType().equals("img") || getType().equals("icon")){
            if(RequestContext.getContext().getFileItemList()!=null){
                FileItem fileitem=RequestContext.getContext().getFileItemList().get(0);
                String fname=getFileName(fileitem.getName());
                String path=RequestContext.getContext().getRealPath(rootPath);
                String type=fname.substring(fname.lastIndexOf(".")+1);
                fileName=sp.format(new Date())+"."+type;
                path+="\\"+fileName;
                FileUpload.getInstance().upload(path,fileitem.getInputStream());
                json.put("fileName", fileName);
            }
            if(getType().equals("img")){
                fileList=FileUpload.getInstance().getFileNameList(RequestContext.getContext().getRealPath(rootPath), new String[]{"gif","jpg","png","bmp"}, true);
                json.put("data", fileList);
            }
            return json.toPageString();
        }else if(getType().equals("file")){
            if(RequestContext.getContext().getFileItemList()!=null){
                FileItem fileitem=RequestContext.getContext().getFileItemList().get(0);
                String fname=getFileName(fileitem.getName());
                String path=RequestContext.getContext().getRealPath(rootPath);
                int n=fname.lastIndexOf(".");
                String type=fname.substring(n+1);
                fileName=fname.substring(0,n)+"("+sp.format(new Date())+")."+type;
                path+="\\"+fileName;
                FileUpload.getInstance().upload(path,fileitem.getInputStream());

                fileList=FileUpload.getInstance().getFileNameList(RequestContext.getContext().getRealPath(rootPath), null, true);
                json.put("fileName", fileName);
                json.put("data", fileList);
                return json.toPageString();
            }

        }
        return "";
    }
    public String delUpFile() throws Exception{
        if(StringUtils.isNotBlank(fileName)){
//    		fileName=fileName.replaceFirst(getRequest().getContextPath(), "");
            new FileUpDao().removeFile(fileName,rootPath);
            getJsonObject().put("state", "ok");
//    		if(fileName.startsWith("/uploadfile")){
//    			File file=new File(SystemConfig.getInstance().getRootPath()+fileName);
//    			if(file.exists())file.delete();
//    			getJsonObject().put("state", "ok");
//    		}
        }
        return getJsonObject().toString();
    }
    private String getFileName(String filename){
        String s=filename.replaceAll("\\/", "\\\\");
        String[] ss=s.split("\\\\");
        return ss[ss.length-1];
    }
    public String getRootPath() {
        return rootPath;
    }

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getActions() {
        return actions;
    }

    public void setActions(String actions) {
        this.actions = actions;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public List getFileList() {
        return fileList;
    }

    public void setFileList(List fileList) {
        this.fileList = fileList;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

}
